/* the extent server implementation
 * Maintains maps at current level
 */

#include "extent_server.h"
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

extent_server::extent_server() {

/* Entry for root of filesystem */
contmap[0x1] = " ";
}

int extent_server::put(extent_protocol::extentid_t id, std::string buf, int &)
{
	if(contmap.find(id) == contmap.end())
	{
		contmap[id] = buf;
		extent_protocol::attr bufAtt;
		bufAtt.size = buf.length();
		time_t tim = time(NULL);
		bufAtt.atime = tim;
		bufAtt.mtime = tim;
		bufAtt.ctime = tim;
		attrmap[id] = bufAtt;
		return extent_protocol::OK;
	}
	return extent_protocol::NOENT;	
}

int extent_server::get(extent_protocol::extentid_t id, std::string &buf)
{
	if(contmap.find(id) != contmap.end())
	{
		buf = contmap[id];
		extent_protocol::attr bufAtt = (attrmap[id]);
		time_t tim = time(NULL);
		bufAtt.atime = tim;
		attrmap[id] = bufAtt;
		return extent_protocol::OK;
	}
	return extent_protocol::NOENT;	
}

int extent_server::getattr(extent_protocol::extentid_t id, extent_protocol::attr &a)
{
  if(attrmap.find(id) != attrmap.end()){
	a = attrmap[id];
	return extent_protocol::OK;
  }else{
	return extent_protocol::NOENT;
  }

}

int extent_server::remove(extent_protocol::extentid_t id, int &)
{
	if(contmap.find(id) != contmap.end() && id != 0x1)
	{
		contmap.erase(id);
		attrmap.erase(id);
		return extent_protocol::OK;
	}
	return extent_protocol::NOENT;
}

